//
//  MainViewController.m
//  拖小延闹钟
//
//  Created by rimi on 15/10/8.
//  Copyright (c) 2015年 Liukai. All rights reserved.
//

#import "MainViewController.h"
#define BUTTON_TAG 100

@interface MainViewController ()
@property (nonatomic, strong) NSArray *viewControllers;
@property (nonatomic, strong) NSArray *barItemImages;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, strong) UIView *childControllerView;
@property (nonatomic, strong) UIView *buttonView;

@end

@implementation MainViewController
- (instancetype)initWithViewControllers:(NSArray *)viewControllers pictureArrray:(NSArray *)pic {
    self = [super init];
    if (self) {
        _selectedIndex = 0;
        _viewControllers = viewControllers;
        _barItemImages = pic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initInterface];
    self.view.backgroundColor = [UIColor yellowColor];
}

- (void)initInterface {
    [self.view addSubview:self.childControllerView];
    [self.view addSubview:self.buttonView];
    if (self.viewControllers.count == 0) {
        return;
    }
    UIViewController *defaultViewController = self.viewControllers.firstObject;
    [self addChildViewController:defaultViewController];
    CGFloat buttonWidth = CGRectGetWidth(self.view.bounds) / self.viewControllers.count;
    NSArray *titleName = @[@"拖延大师",@"起床管家",@"社区成就"];
    [self.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(buttonWidth * idx, 0, buttonWidth, 60);
        button.tag = BUTTON_TAG + idx;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [button setTitle:titleName[idx] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(handleEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonView addSubview:button];
    }];
    UIButton *defaultButton = (UIButton *)[self.buttonView viewWithTag:100];
    defaultButton.selected = YES;

}

- (void)handleEvent:(UIButton *)sender {
    NSInteger index = sender.tag - BUTTON_TAG;
    if (_selectedIndex == index) {
        return;
    }
//    if (index != 0 && self.view.subviews.count > 2) {
//        [self.view.subviews.lastObject removeFromSuperview];
//    }
    UIViewController *selectedViewController = self.viewControllers[_selectedIndex];
    [selectedViewController willMoveToParentViewController:nil];
    [selectedViewController removeFromParentViewController];
    [selectedViewController.view removeFromSuperview];
    UIViewController *currentViewController = self.viewControllers[index];
    [self addChildViewController:currentViewController];
    UIButton *selectedButton = (UIButton *)[self.buttonView viewWithTag:self.selectedIndex + BUTTON_TAG];
    selectedButton.selected = NO;
    sender.selected = YES;
    self.selectedIndex = index;
}
#pragma mark -- Getter 
- (UIView *)childControllerView {
    if (!_childControllerView) {
        _childControllerView = ({
            UIView *aView = [[UIView alloc] initWithFrame:self.view.frame];
            aView;
        });
    }
    return _childControllerView;
}

- (UIView *)buttonView {
    if (!_buttonView) {
        _buttonView = ({
            UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 60, CGRectGetWidth(self.view.bounds), 60)];
            aView.backgroundColor = [UIColor colorWithWhite:0.929 alpha:1.000];
            aView;
        });
    }
    return _buttonView;
}


- (void)addChildViewController:(UIViewController *)childController {
    [super addChildViewController:childController];
    [self.childControllerView addSubview:childController.view];
    [self didMoveToParentViewController:self];
}


@end
